<template>
  <el-form :model="props.queryParams" ref="queryForm" :inline="true" label-width="100px">
    <el-form-item :label="item.label" prop="name" v-for="(item, index) in props.data" :key="index" :label-width="item.labelWidth?item.labelWidth+'px':'100px'">

      <el-input v-model="props.queryParams[item.name]" placeholder="请输入" clearable @keyup.enter="handleQuery" v-if="item.type == 'text'" />
      <el-select v-if="item.type == 'select'" v-model="props.queryParams[item.name]" placeholder="请选择" clearable>
        <el-option v-for="dict in item.data" :key="dict.value" :label="dict.label" :value="dict.value" />
      </el-select>
      <el-date-picker v-if="item.type == 'date'" is-range v-model="props.queryParams[item.name]" type="date" class="full-width-input" format="YYYY-MM-DD"
        value-format="YYYY-MM-DD" clearable></el-date-picker>

      <el-date-picker v-if="item.type == 'dateAll'" v-model="props.queryParams[item.name]" type="daterange" range-separator="To" start-placeholder="开始日期"
        end-placeholder="结束日期" :size="size" />

      <el-tree-select v-if="item.type == 'tree'" value-key="id" v-model="props.queryParams[item.name]" style="width:100%" check-strictly clearable
        :data="item.data" />

    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import moment from 'moment'
const emit = defineEmits(["getList", 'delQuery', 'handleQuery']);
const props = defineProps({
  data: {
    type: Array,
  },
  queryParams: {
    type: Object,
  },
  isSearch: {
    type: Boolean
  }

});

const { proxy } = getCurrentInstance();
const queryForm = ref()

/** 搜索按钮操作 */
function handleQuery() {
  let arr = props.data.filter(item => item.type == 'dateAll')
  if (arr && arr.length > 0 && props.queryParams.timeall) {
    if (!props.isSearch) {
      props.queryParams.timeall[0] = moment(props.queryParams.timeall[0]).format('YYYY-MM-DD')
      props.queryParams.timeall[1] = moment(props.queryParams.timeall[1]).format('YYYY-MM-DD')
    } else {
      delete props.queryParams.timeall[0]
      delete props.queryParams.timeall[1]
      props.queryParams[arr[0].data[0]] = moment(props.queryParams.timeall[0]).format('YYYY-MM-DD')
      props.queryParams[arr[0].data[1]] = moment(props.queryParams.timeall[1]).format('YYYY-MM-DD')
    }





  }
  // timestampToTime()

  emit("handleQuery", props.queryParams);
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");

  emit("delQuery");
  handleQuery();
}

const timestampToTime = (timestamp) => {
  // 创建一个Date对象，参数为时间戳乘以1000（转换为毫秒，是毫秒就不用再乘1000）
  let date = new Date(timestamp * 1000)
  // 或let date=new Date(timestamp)
  // 使用Date对象的各种方法获取年、月、日、时、分、秒等信息
  let year = date.getFullYear()
  let month = date.getMonth() + 1//月份是从0开始计数的，所以要加1
  let day = date.getDate()
  let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
  let minute = date.getHours() < 10 ? "0" + date.getMinutes() : date.getMinutes()
  let second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
  // 拼成需要的时间格式
  let time = year + "-" + month + "-" + day + " " + hour + "-" + minute + "-" + second
  return time
}

</script>

<style></style>
